﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace XHCTMS.View
{
    public partial class Teach : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session["Teaching"] = null;
                ViewState["teachingId"] = null;
                ViewState["PageIndex"] = 1;
                ViewState["PageSize"] = this.ddlPageSize.Text;
                ViewState["MaxPage"] = new BLL.Teaching().GetCountPage(ChangePageSize());
                PageChange(true);

                DDLBind();
                ShowData(Convert.ToInt32(ViewState["PageSize"]), 1);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                XHCTMS.MODEL.Teaching teaching = new MODEL.Teaching();
                teaching.TeachingLesson = this.txtLesson.Text;
                teaching.TeachingTeachId = Convert.ToInt32(this.ddlTeacher.Text);
                teaching.TeachingClassId = Convert.ToInt32(this.ddlClass.Text);
                int result = new BLL.Teaching().AddTeaching(teaching);
                int page = Convert.ToInt32(new BLL.Teaching().GetCountPage(ChangePageSize()));
                int maxPage = Convert.ToInt32(new BLL.Teaching().GetCountPage(ChangePageSize()));
                ViewState["MaxPage"] = maxPage;
                PageChange(true);
                ShowData(Convert.ToInt32(ViewState["PageSize"]), Convert.ToInt32(ViewState["PageIndex"]));
                ClearText();
            }
            catch
            {                
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            XHCTMS.MODEL.Teaching teaching = new MODEL.Teaching();
            if (!string.IsNullOrEmpty(txtLesson.Text))
            {
                teaching.TeachingLesson = this.txtLesson.Text;
            }
            if (!string.IsNullOrEmpty(ddlTeacher.Text))
            {
                teaching.TeachingTeachId = Convert.ToInt32(this.ddlTeacher.Text);
            }
            if (!string.IsNullOrEmpty(ddlClass.Text))
            {
                teaching.TeachingClassId = Convert.ToInt32(this.ddlClass.Text);
            }
            if (!string.IsNullOrEmpty(ddlState.Text))
            {
                teaching.TeachingState = Convert.ToInt32(this.ddlState.Text);
            }
            DataTable dt = new BLL.Teaching().SearchTeaching(teaching, ChangePageSize(), ChangePageIndex());
            DataTable dtPage = new BLL.Teaching().GetCountPage(teaching);
            int pageSize = Convert.ToInt32(dtPage.Rows[0][0].ToString());
            if (pageSize / ChangePageSize() < 1)
            {
                ViewState["MaxPage"] = 1;
            }
            else
            {
                ViewState["MaxPage"] = pageSize % ChangePageSize() == 0 ? pageSize / ChangePageSize() : pageSize / ChangePageSize() + 1;
            }
            Session["Teaching"] = teaching;
            ViewState["PageIndex"] = 1;
            ShowData(teaching, ChangePageSize(), ChangePageIndex());
            PageChange(true);
            ClearText();
        }

        protected void btnRevise_Click(object sender, EventArgs e)
        {
            try
            {
                int teachingId = Convert.ToInt32(Session["teachingId"]);
                MODEL.Teaching teaching = new MODEL.Teaching();
                teaching.TeachingLesson = this.txtLesson.Text;
                teaching.TeachingTeachId = Convert.ToInt32(this.ddlTeacher.Text);
                teaching.TeachingClassId = Convert.ToInt32(this.ddlClass.Text);
                teaching.TeachingState = Convert.ToInt32(this.ddlState.Text);
                int result = new BLL.Teaching().ReviseTeaching(teachingId, teaching);
                ShowData(Convert.ToInt32(ViewState["PageSize"]), Convert.ToInt32(ViewState["PageIndex"]));
                ClearText();
            }
            catch
            {
            }
        }

        protected void infolist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int teachingId = Convert.ToInt32(e.CommandArgument.ToString());
            Session["teachingId"] = teachingId;
            switch (e.CommandName)
            {
                case "linkbtnRevise":
                    MODEL.Teaching teaching = new MODEL.Teaching();
                    teaching.TeachingId = teachingId;
                    DataTable dt = new BLL.Teaching().SearchTeaching(teaching, ChangePageSize(), ChangePageIndex());
                    this.txtLesson.Text = dt.Rows[0][3].ToString();
                    this.ddlClass.Text = dt.Rows[0][6].ToString();
                    this.ddlTeacher.Text = dt.Rows[0][7].ToString();
                    this.ddlState.Text = dt.Rows[0][5].ToString();
                    break;
                case "linkbtnDelete":
                    int result = new BLL.Teaching().Guitarlessons(teachingId);
                    ShowData(Convert.ToInt32(ViewState["PageSize"]), Convert.ToInt32(ViewState["PageIndex"]));
                    break;
            }
        }             

        protected void btnPrevious_Click(object sender, EventArgs e)
        {            
            Pagination(1);            
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            Pagination(0);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            Pagination(2);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            Pagination(3);
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);
            int pageSize = ChangePageSize();
            MODEL.Teaching teaching;
            if (Session["Teaching"] == null)
            {
                teaching = new MODEL.Teaching();
            }
            else
            {
                teaching = Session["Teaching"] as MODEL.Teaching;
            }
            ViewState["PageIndex"] = pageIndex;
            ShowData(teaching, pageSize, pageIndex);            
        }

        private void Pagination(int state)
        {
            int pageSize = ChangePageSize();
            int pageIndex = 1;
            MODEL.Teaching teaching;
            if (Session["Teaching"] == null)
            {
                teaching = new MODEL.Teaching();
            }
            else
            {
                teaching = Session["Teaching"] as MODEL.Teaching;
            }
            switch (state)
            {
                //下一页
                case 0:
                    pageIndex = ChangePageIndex() + 1;
                    int maxPage = Convert.ToInt32(ViewState["MaxPage"]);
                    this.ddlPage.SelectedIndex += 1;
                    break;
                //上一页
                case 1:
                    pageIndex = ChangePageIndex() - 1;
                    this.ddlPage.SelectedIndex -= 1;
                    break;
                //首页
                case 2:
                    pageIndex = 1;
                    this.ddlPage.SelectedIndex = pageIndex - 1;
                    break;
                //尾页
                case 3:
                    pageIndex = Convert.ToInt32(new BLL.Teaching().GetCountPage(pageSize));
                    this.ddlPage.SelectedIndex = pageIndex - 1;
                    break;
                default:
                    break;
            }

            ViewState["PageIndex"] = pageIndex;
            ShowData(teaching, pageSize, pageIndex);
        }

        private void PageChange(bool IsFirst)
        {
            try
            {
                int maxPage = Convert.ToInt32(ViewState["MaxPage"]);
                List<string> page = new List<string> { };
                for (int i = 1; i <= maxPage; ++i)
                {
                    page.Add(i.ToString() + "/" + maxPage.ToString());
                }
                this.ddlPage.DataSource = page;
                this.ddlPage.DataBind();
                if (IsFirst == true)
                {
                    this.ddlPage.SelectedIndex = ChangePageIndex() - 1;
                }
                else
                {
                    this.ddlPage.SelectedIndex = 0;
                    ViewState["PageIndex"] = 1;
                }
            }
            catch
            {
            }
        }      

        private void ShowData(int pageSize, int pageIndex)
        {            
            MODEL.Teaching teaching = new MODEL.Teaching();
            DataTable dt = new XHCTMS.BLL.Teaching().SearchTeaching(teaching, pageSize, pageIndex);
            this.infolist.DataSource = dt;
            infolist.DataBind();
        }
        private void ShowData(MODEL.Teaching teaching,  int pageSize, int pageIndex)
        {
            DataTable dt = new XHCTMS.BLL.Teaching().SearchTeaching(teaching, pageSize, pageIndex);
            this.infolist.DataSource = dt;
            infolist.DataBind();
        }

        private void ClearText()
        {
            this.txtLesson.Text = null;
            this.ddlClass.SelectedIndex = 0;
            this.ddlTeacher.SelectedIndex = 0;
            this.ddlState.SelectedIndex = 0;
        }

        private int ChangePageSize()
        {
            return Convert.ToInt32(ViewState["PageSize"]);
        }
        private int ChangePageIndex()
        {
            return Convert.ToInt32(ViewState["PageIndex"]);
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int pageSize = Convert.ToInt32(this.ddlPageSize.Text);
                ViewState["PageSize"] = pageSize;
                ViewState["MaxPage"] = new BLL.Teaching().GetCountPage(pageSize);
                ShowData(pageSize, 1);
                PageChange(false);
            }
            catch
            {
            }
        }

        private void DDLBind()
        {
            DataTable dtDDLClass = new BLL.Teaching().GetClassName();
            ddlClass.Items.Clear();
            ddlClass.DataSource = dtDDLClass;
            ddlClass.DataTextField = "class_name";
            ddlClass.DataValueField = "class_id";
            ddlClass.DataBind();
            ddlClass.Items.Insert(0, new ListItem("", ""));

            DataTable dtDDLTeacher = new BLL.Teaching().GetTeacherName();
            ddlTeacher.Items.Clear();
            ddlTeacher.DataSource = dtDDLTeacher;
            ddlTeacher.DataTextField = "user_name";
            ddlTeacher.DataValueField = "user_id";
            ddlTeacher.DataBind();
            ddlTeacher.Items.Insert(0, new ListItem("", ""));

            ddlState.Items.Clear();
            ddlState.Items.Insert(0, new ListItem("", ""));
            ddlState.Items.Insert(1, new ListItem("在授", "1"));
            ddlState.Items.Insert(1, new ListItem("结课未打分", "0"));
            ddlState.Items.Insert(1, new ListItem("已打分", "-1"));
        }
    }
}